package org.jeecg.modules.api.controller.music.comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.dto.CommentDto;
import org.jeecg.modules.api.vo.comment.CommentVo;
import org.jeecg.modules.music.entity.MusComment;
import org.jeecg.modules.music.service.IMusCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags="API-音乐系统")
@RestController
@RequestMapping("api/mus/")
public class CommentCenterController {

    @Autowired
    private IMusCommentService commentService;

    @ApiOperation(value = "获取评论列表", notes = "获取评论列表", response = String.class)
    @PostMapping(value = "/comment/list")
    public Result queryCommentList(@RequestBody CommentVo commentVo){
        log.info("获取评论列表: {}", commentVo);
        Map<String,Object> mp = new HashMap<>();
        IPage<MusComment> page = commentService.getPageList(commentVo);
        mp.put("page",page);
        return Result.ok(mp);
    }

    @ApiOperation(value = "增加评论", notes = "增加评论")
    @PostMapping("/comment/add")
    public Result addComment(@RequestBody CommentVo commentVo){
        log.info("新增评论: {}", commentVo);
        Boolean flag = commentService.addComment(commentVo);
        if (flag){
            return Result.ok("操作成功！");
        }
        return Result.error("操作失败！");
    }

    @ApiOperation(value = "评论列表", notes = "评论列表")
    @RequestMapping(value = "/comment/hot",method = RequestMethod.GET)
    public Result commentHot(@RequestParam(required = true,name = "uid") String uid,
                                               @RequestParam(name = "type",required = true) Integer type,
                                               @RequestParam(name = "limit",required = true) Integer limit,
                                               @RequestParam(name = "offset",required = true) Integer offset){
        List<CommentDto> list =commentService.queryHotComments(uid,type,limit,offset);
        Map<String,Object> mp = new HashMap<>();
        mp.put("comments",list);
        mp.put("total",list.size());
        return Result.ok(mp);
    }

    @ApiOperation(value = "remove评论", notes = "remove评论")
    @RequestMapping(value = "/comment/remove",method = RequestMethod.GET)
    public Result deleteCommentAPI(@RequestParam(name = "uid",required = true) String uid,
                                   @RequestParam(name = "userUid",required = true) String userUid,
                                   @RequestParam(name = "targetUid",required = true) String targetUid){
        if (StringUtils.isNotEmpty(uid)&&StringUtils.isNotEmpty(userUid)&&StringUtils.isNotEmpty(targetUid)){
            commentService.removeComment(uid,userUid,targetUid);
            return Result.ok();
        }

        return Result.ok();
    }
}
